//
//  ZZDatePickerView.m
//  ZZKit
//
//  Created by 袁亮 on 16/5/12.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "ZZDatePickerView.h"

@interface ZZDatePickerView()

@property (strong,nonatomic) UIButton *doneBtn;
@property (strong,nonatomic) UIDatePicker *datepicker;
@property (strong,nonatomic) NSString *startDate;

@property (assign, nonatomic) NSString *dateFormat;

@property (weak, nonatomic) id <ZZDatePickerViewDelegate> delegate;

@end

@implementation ZZDatePickerView


+ (ZZDatePickerView *)sharedPicker
{
    static ZZDatePickerView *pickerView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        pickerView = [[self alloc] init];
    });
    return pickerView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = RGBA(0, 0, 0, 0.7);
        
//        [self makeDoneButtonUI];
        
        [self makeDatePickerUI];
        
    }
    return self;
}

- (void) makeDoneButtonUI
{
    self.doneBtn = [[UIButton alloc]initWithFrame:CGRectMake(KW - 55, 5, 50, 20)];
    [self.doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.doneBtn setTitleColor:RGB(64, 187, 254) forState:UIControlStateNormal];
    [self.doneBtn addTarget:self action:@selector(doneButtonMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.doneBtn];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 29, KW, 1)];
    line.backgroundColor = RGB(220, 220, 220);

    [self addSubview:line];
}



- (void) makeDatePickerUI
{
    _datepicker = [UIDatePicker new];
    _datepicker.backgroundColor = [UIColor whiteColor];
    NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"Chinese"];
    _datepicker.datePickerMode = UIDatePickerModeDate;
    [_datepicker setLocale:locale];
    _datepicker.minimumDate = [NSDate date];
    [_datepicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];
    [self addSubview:_datepicker];
    [_datepicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(self.mas_height).multipliedBy(0.5);
    }];
    
    [self instance];
}

- (void)instance
{
    NSString *dateString = [ZZDateFormat fetch10Date:[ZZDateFormat fetchUnixDate:_datepicker.date] format:@"yyyyMMdd"];
    
    self.startDate = [ZZDateFormat fetchUnixDate:dateString format:@"yyyyMMdd"];
}

- (void)dateChanged:(id)sender{
    
    UIDatePicker* control = (UIDatePicker*)sender;
    
    NSString *dateString = [ZZDateFormat fetch10Date:[ZZDateFormat fetchUnixDate:control.date] format:@"yyyyMMdd"];
    
    self.startDate = [ZZDateFormat fetchUnixDate:dateString format:@"yyyyMMdd"];
}

- (void)doneButtonMethod
{
    

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (self.mode == TaskDateModeOfStart) {
        if ([self.delegate respondsToSelector:@selector(didSelectDatePickerWithMode:date:)]) {
            [self.delegate didSelectDatePickerWithMode:TaskDateModeOfStart date:self.startDate];
        }
        [self removeFromSuperview];
    }else if (self.mode == TaskDateModeOfEnd){
        if ([self.delegate respondsToSelector:@selector(didSelectDatePickerWithMode:date:)]) {
            [self.delegate didSelectDatePickerWithMode:TaskDateModeOfEnd date:self.startDate];
        }
        [self removeFromSuperview];
    }
}

- (void)showPickerWithMode:(TaskDateMode)mode delegate:(id<ZZDatePickerViewDelegate>)delegate
{
    self.mode = mode;
    self.delegate = delegate;
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
    
}


@end
